app.register.controller("UsstkDetailsCtrl", ["$rootScope", "$scope", "$location", "$timeout", "$window", "ToastMessage", "SecurityService", "FundService", "StockService", function($rootScope, $scope, $location, $timeout, $window, ToastMessage, SecurityService, FundService, StockService) {
    StockService.setModule(8003),
    $scope.currentTab = "today",
    $scope.todayRunning = !1,
    $scope.loadSearchResult = function() {
        $scope.historyDones = [],
        $scope.historyPageNumber = 0,
        funs.loadHistoryNextPage()
    }
    ,
    $scope.todayDones = [],
    $scope.historyDones = [],
    $scope.getGenMoney = function(item) {
        return item ? "BUY" == item.orderType ? item.doneMoney + item.taxesActual + item.commissionActual : item.doneMoney - item.taxesActual - item.commissionActual : "-"
    }
    ,
    $scope.selectTab = function(tab) {
        $scope.currentTab = tab,
        $scope.todayDones = [],
        $scope.historyDones = [],
        "today" == tab ? ($scope.todayPageNumber = 0,
        funs.loadTodayNextPage()) : "history" == tab && ($scope.historyPageNumber = 0,
        funs.loadHistoryNextPage())
    }
    ,
    $scope.loadSearchResult = function() {
        $scope.historyDones = [],
        $scope.historyPageNumber = 0,
        funs.loadHistoryNextPage()
    }
    ,
    $scope.getFormatDate = function(time) {
        if (!time)
            return "";
        var mdhm = (time + "").substr(4, 8);
        if (mdhm) {
            var mon = mdhm.substr(0, 2)
              , day = mdhm.substr(2, 2)
              , hours = mdhm.substr(4, 2)
              , min = mdhm.substr(6, 2)
              , today = mon + "-" + day + " " + hours + ":" + min;
            return today
        }
        return ""
    }
    ;
    var funs = {
        getTodayPageNumber: function() {
            if ($scope.todayPageNumber) {
                var page = $scope.todayPageNumber;
                return page + 1
            }
            return 1
        },
        getTodayTotalPages: function() {
            return $scope.todayTotalPages ? $scope.todayTotalPages : 1
        },
        getHistoryPageNumber: function() {
            if ($scope.historyPageNumber) {
                var page = $scope.historyPageNumber;
                return page + 1
            }
            return 1
        },
        getHistoryTotalPages: function() {
            return $scope.historyTotalPages ? $scope.historyTotalPages : 1
        },
        loadTodayDones: function() {
            var ssso = {
                startDate: funs.getToday()
            };
            StockService.listBillOrders(8003, ssso).success(function(data) {
                data && data.success && app.util.formatListStatusData($scope, data.result, "listBills")
            })
        },
        loadTodayNextPage: function(event) {
            if (!$scope.todayRunning) {
                if (funs.getTodayTotalPages() < funs.getTodayPageNumber() || "history" == $scope.currentTab)
                    return void ($scope.showMore = !1);
                $scope.todayRunning = !0;
                var ssso = {
                    startDate: funs.getToday(),
                    totalPages: funs.getTodayTotalPages(),
                    pageNumber: funs.getTodayPageNumber()
                };
                event && app.util.loading(event.target, !0),
                StockService.listBillOrders(8003, ssso).success(function(data) {
                    event && app.util.loading(event.target, !1),
                    $scope.todayRunning = !1,
                    data && data.success && ($scope.todayPageNumber = data.result.pageNumber,
                    $scope.todayTotalPages = data.result.totalPages,
                    !data.result || !data.result.list || data.result.list.length <= 0 ? ($scope.todayDonesNodata = !0,
                    $scope.showMore = !1) : ($scope.todayDonesNodata = !1,
                    $scope.todayDones = $scope.todayDones.concat(data.result.list),
                    $scope.showMore = 10 == data.result.list.length))
                })
            }
        },
        loadHistoryNextPage: function(event) {
            if (!$scope.historyRunning) {
                if (funs.getHistoryTotalPages() < funs.getHistoryPageNumber() || "today" == $scope.currentTab)
                    return void ($scope.showHistoryMore = !1);
                $scope.historyRunning = !0;
                var ssso = {
                    startDate: funs.getToday($scope.startDate),
                    endDate: funs.getToday($scope.endDate),
                    totalPages: funs.getHistoryTotalPages(),
                    pageNumber: funs.getHistoryPageNumber()
                };
                event && app.util.loading(event.target, !0),
                StockService.listBillOrders(8003, ssso).success(function(data) {
                    event && app.util.loading(event.target, !1),
                    $scope.historyRunning = !1,
                    data && data.success && ($scope.historyPageNumber = data.result.pageNumber,
                    $scope.historyTotalPages = data.result.totalPages,
                    !data.result || !data.result.list || data.result.list.length <= 0 ? $scope.historyDonesNodata = !0 : ($scope.historyDonesNodata = !1,
                    $scope.historyDones = $scope.historyDones.concat(data.result.list),
                    $scope.showHistoryMore = 10 == data.result.list.length))
                })
            }
        },
        loadDones: function() {
            var ssso = {
                startDate: funs.getToday($scope.startDate),
                endDate: funs.getToday($scope.endDate)
            };
            StockService.listBillOrders(8003, ssso).success(function(data) {
                data && data.success && app.util.formatListStatusData($scope, data.result, "historyBills")
            })
        },
        getToday: function(d) {
            var date = d || new Date
              , mon = date.getMonth() + 1
              , day = date.getDate()
              , today = date.getFullYear() + "-" + (10 > mon ? "0" + mon : mon) + "-" + (10 > day ? "0" + day : day);
            return today
        }
    };
    $scope.startDate = new Date(funs.getToday()),
    $scope.endDate = new Date(funs.getToday()),
    $scope.loadTodayNextPage = funs.loadTodayNextPage,
    $scope.loadHistoryNextPage = funs.loadHistoryNextPage,
    $scope.loadTodayNextPage()
}
]);
